package com.work.dyduser.httpretrofit;

import com.work.dyduser.utils.JsonUtils;
import com.work.dyduser.utils.LogUtils;
import com.work.dyduser.utils.UseRSAUtil;

import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * author：WangZhan
 * data: 2017/3/29 17:19
 * version: 1.0.0
 * explain:统一的post发送请求类
 */

public class PostBody {


    /**
     * 获取请求体 Requestbody
     */
    public static RequestBody toBody(Map<String, String> map) {
        LogUtils.d("请求体=" + JsonUtils.parseBeantojson(map));
//        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JsonUtils.parseBeantojson(map));
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), JsonUtils.parseBeantojson(map));
        return requestBody;
    }

    //获取加密请求体 String
    public static String toBodyPassword(Map<String, Object> map) {
        LogUtils.d("请求体=" + JsonUtils.parseBeantojson(map));
        String requestStr = null;
        try {
            requestStr = UseRSAUtil.encryptByPublicKey(JsonUtils.parseBeantojson(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
//      LogUtils.d("加密数据Str=" + requestStr);
        return requestStr;
    }

/*
    //比如可以这样生成Map<String, RequestBody> requestBodyMap
    //Map<String, String> requestDataMap这里面放置上传数据的键值对。
    public static Map<String, RequestBody> generateRequestBody(Map<String, String> requestDataMap) {
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        for (String key : requestDataMap.keySet()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
            requestBodyMap.put(key, requestBody);
        }
        return requestBodyMap;
    }

    //推荐使用这种方式，把请求的数据集都放在map中，这样统一处理起来非常的方便。
//    @Multipart
//    @POST("login")
//    Call<Object> userLogin(@PartMap Map<String, RequestBody> requestBodyMap);
*/

}
